<?php

namespace app\admin\controller\work;

use think\App;
use app\Request;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;

#[ControllerAnnotation(title: '考勤统计')]
class AttendanceStatistics extends AdminController
{

    private array $notes;
    
    #[NodeAnnotation(ignore: ['add', 'delete', 'edit', 'export', 'modify', 'recycle'])] // 过滤不需要生成的权限节点
    protected array $ignoreNode;
    
    protected array $sort = [
        'statistics_date' => 'desc',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        self::$model = new \app\admin\model\WorkAttendanceStatistics();
        $notes = self::$model::$notes;

        $this->notes = $notes;
        $this->assign(compact('notes'));
    }

    #[NodeAnnotation(title: '添加', auth: false)]
    public function add(Request $request): string
    {
        $this->error('系统自动统计考勤数据，无需手动添加');
    }

    #[NodeAnnotation(title: '删除', auth: true)]
    public function delete(Request $request): void
    {
        $this->checkPostRequest();
        $id  = $request->param('id');
        $row = self::$model::whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        $date = $row[0]["'date'"];
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除异常');
        }

        if ($save) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    #[NodeAnnotation(title: '编辑', auth: false)]
    public function edit(Request $request, $id = 0): string
    {
       $this->error('系统自动统计考勤数据，无需手动编辑');
    }
}
